package controller;

import model.Blog;
import model.BlogDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/blogDelete")
public class BlogDeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        String blogId = req.getParameter("blogId");
        if (blogId == null || "".equals(blogId)){
            resp.getWriter().write("当前博客不存在!");
            return;
        }
        BlogDao blogDao = new BlogDao();
        Blog blog = blogDao.selectOneBlog(Integer.parseInt(blogId));
        if (blog == null){
            resp.getWriter().write("当前博客不存在!");
            return;
        }

        blogDao.deleteBlog(Integer.parseInt(blogId));
        resp.sendRedirect("list.html");
    }
}
